package mvc;
import geographer.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextField;

/**
 * Project #4
 * CS 2334, 010
 * 04/02/2014
 * <P>
 * This class represents a dialog to edit an object to the database.
 * </P>
 * @version 1.0
 */
public class EditDialog extends JFrame implements ActionListener
{
	/*The text areas for the user to input the data*/
	JTextField nameEntry;
	JTextField areaEntry;
	JTextField populationEntry;
	JTextField latitudeEntry;
	JTextField longitudeEntry;
	JTextField elevationEntry;
	JTextField descriptionEntry;
	JTextField belongingEntry;
	
	/*The button for the user to hit to add the event*/
	static JButton editButton;
	
	/* The Region enum from the Database class */
	Database.Region r;
	
	/* A String key */
	String key;
	
	/* The current view */
	SelectionView view;
	
	/**
	 * The constructor for the AddDialog that builds the GUI and adds functionality.
	 * @param region   The region that is being added.
	 */
	public EditDialog(Database.Region r, SelectionView parent, String key){
		this.r = r;
		view = parent;
		this.key = key;
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(8,1));
		JPanel buttonPanel = new JPanel();
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(8,1));
		
		JLabel nameLabel = new JLabel("Name: ");
		JLabel areaLabel = new JLabel("Area: ");
		JLabel populationLabel = new JLabel("Population: ");
		JLabel latLabel = new JLabel("Latitude: ");
		JLabel lonLabel = new JLabel("Longitude: ");
		JLabel elevLabel = new JLabel("Elevation: ");
		JLabel descriptionLabel = new JLabel("Description: ");
		JLabel belongingLabel = new JLabel("Belongs To: ");

		nameEntry = new JTextField(1);
		nameEntry.setEnabled(false);
		areaEntry = new JTextField(1);
		areaEntry.setEnabled(false);
		populationEntry = new JTextField(1);
		populationEntry.setEnabled(false);
		latitudeEntry = new JTextField(1);
		latitudeEntry.setEnabled(false);
		longitudeEntry = new JTextField(1);
		longitudeEntry.setEnabled(false);
		elevationEntry = new JTextField(1);
		elevationEntry.setEnabled(false);
		descriptionEntry = new JTextField(1);
		descriptionEntry.setEnabled(false);
		belongingEntry = new JTextField(1);
		belongingEntry.setEnabled(false);

		if(r == Database.Region.CONTINENT)
		{
			Continent cont = (Continent)Database.regions.get(key);
			labelPanel.add(nameLabel);
			labelPanel.add(populationLabel);
			labelPanel.add(areaLabel);
			
			inputPanel.add(nameEntry);
			nameEntry.setEnabled(true);
			nameEntry.setText(cont.getName());
			inputPanel.add(populationEntry);
			populationEntry.setEnabled(true);
			populationEntry.setText(cont.getPopulation());
			inputPanel.add(areaEntry);
			areaEntry.setEnabled(true);
			areaEntry.setText(cont.getArea());
		}
		else if(r == Database.Region.CITY)
		     {
				City city = (City)Database.regions.get(key);
				labelPanel.add(nameLabel);
				labelPanel.add(populationLabel);
				labelPanel.add(areaLabel);
				labelPanel.add(latLabel);
				labelPanel.add(lonLabel);
				labelPanel.add(elevLabel);
				labelPanel.add(belongingLabel);
				
				inputPanel.add(nameEntry);
				nameEntry.setEnabled(true);
				nameEntry.setText(city.getName());
				inputPanel.add(populationEntry);
				populationEntry.setEnabled(true);
				populationEntry.setText(city.getPopulation());
				inputPanel.add(areaEntry);
				areaEntry.setEnabled(true);
				areaEntry.setText(city.getArea());
				inputPanel.add(latitudeEntry);
				latitudeEntry.setEnabled(true);
				latitudeEntry.setText(city.getLat());
				inputPanel.add(longitudeEntry);
				longitudeEntry.setEnabled(true);
				longitudeEntry.setText(city.getLon());
				inputPanel.add(elevationEntry);
				elevationEntry.setEnabled(true);
				elevationEntry.setText(city.getElevation());
				inputPanel.add(belongingEntry);
				belongingEntry.setEnabled(true);
				belongingEntry.setText(city.getSuperRegion());
		     }
		else if(r == Database.Region.COUNTRY)
			 {
				Country country = (Country)Database.regions.get(key);
				labelPanel.add(nameLabel);
				labelPanel.add(populationLabel);
				labelPanel.add(areaLabel);
				labelPanel.add(belongingLabel);
				
				inputPanel.add(nameEntry);
				nameEntry.setEnabled(true);
				nameEntry.setText(country.getName());
				inputPanel.add(populationEntry);
				populationEntry.setEnabled(true);
				populationEntry.setText(country.getPopulation());
				inputPanel.add(areaEntry);
				areaEntry.setEnabled(true);
				areaEntry.setText(country.getArea());
				inputPanel.add(belongingEntry);
				belongingEntry.setEnabled(true);
				belongingEntry.setText(country.getSuperRegion());
			 }
		else if(r == Database.Region.POINT)
			 {
				Point point = (Point)Database.regions.get(key);
				labelPanel.add(nameLabel);
				labelPanel.add(descriptionLabel);
				labelPanel.add(latLabel);
				labelPanel.add(lonLabel);
				labelPanel.add(elevLabel);
				labelPanel.add(belongingLabel);
				
				inputPanel.add(nameEntry);
				nameEntry.setEnabled(true);
				nameEntry.setText(point.getName());
				inputPanel.add(descriptionEntry);
				descriptionEntry.setEnabled(true);
				descriptionEntry.setText(point.getDescription());
				inputPanel.add(latitudeEntry);
				latitudeEntry.setEnabled(true);
				latitudeEntry.setText(point.getLat());
				inputPanel.add(longitudeEntry);
				longitudeEntry.setEnabled(true);
				longitudeEntry.setText(point.getLon());
				inputPanel.add(elevationEntry);
				elevationEntry.setEnabled(true);
				elevationEntry.setText(point.getElevation());
				List<String> list = point.getBelonging();
				String text = "";
				for(String item:list)
				{
					text+=(item+",");
				}
				inputPanel.add(belongingEntry);
				belongingEntry.setEnabled(true);
				belongingEntry.setText(text);
				
		     }
		else if(r == Database.Region.PLACE)
			 {
				Place place = (Place)Database.regions.get(key);
				labelPanel.add(nameLabel);
				labelPanel.add(areaLabel);
				labelPanel.add(belongingLabel);
				labelPanel.add(descriptionLabel);
				
				inputPanel.add(nameEntry);
				nameEntry.setEnabled(true);
				nameEntry.setText(place.getName());
				inputPanel.add(areaEntry);
				areaEntry.setEnabled(true);
				areaEntry.setText(place.getArea());
				inputPanel.add(belongingEntry);
				List<String> list = place.getOwners();
				String text = "";
				for(String item:list)
				{
					text+=(item+",");
				}
				belongingEntry.setEnabled(true);
				belongingEntry.setText(text);
				inputPanel.add(descriptionEntry);
				descriptionEntry.setEnabled(true);
				descriptionEntry.setText(place.getDescription());
			 }
		
		editButton = new JButton("Edit");
		editButton.addActionListener(this);
		buttonPanel.add(editButton);
		
		this.setTitle("Edit "+r);
		this.setSize(500, 300);
		this.add(labelPanel, BorderLayout.WEST);
		this.add(buttonPanel, BorderLayout.SOUTH);
		this.add(inputPanel, BorderLayout.CENTER);
		this.setVisible(true);
	}//end constructor
	
	/**
	 * The action listener for the class. Listens and validates parameters.
	 * @param	e	The Actionevent
	 */
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == editButton)
		{
			List<String> data = new ArrayList<String>();
			data.add(nameEntry.getText());
			if(descriptionEntry.isEnabled())
			{
				data.add(descriptionEntry.getText());
			}
			if(populationEntry.isEnabled())
			{
				data.add(populationEntry.getText());
			}
			if(areaEntry.isEnabled())
			{
				data.add(areaEntry.getText());
			}
			if(latitudeEntry.isEnabled())
			{
				data.add(latitudeEntry.getText());
			}
			if(longitudeEntry.isEnabled())
			{
				data.add(longitudeEntry.getText());
			}
			if(elevationEntry.isEnabled())
			{
				data.add(elevationEntry.getText());
			}
			if(belongingEntry.isEnabled())
			{
				String[] subdata = belongingEntry.getText().split(",");
				for(int j = 0;j<subdata.length;++j)
				{
					data.add(subdata[j]);
				}
			}

			String[] finalData = new String[data.size()];
			for(int i = 0;i<finalData.length;++i)
			{
				finalData[i] = data.get(i);
				
			}
			this.view.getModel().edit(r, finalData, key);
		}
	}
}
